<?php

include_once("dbconnect.php");
$sex = "";
$pq = "";

if(isset($_GET['type'])){
    if($_GET['type']=="reset"){
        resetPass($_GET['email']);
        header("location: ../users.php?status=resetSuccess");
    }
}

function resetPass($email){
    $passdefault = md5('123456');
    mysqli_query(getConnect(), "update users set password = '$passdefault' where email='$email'");
}

function printTable() {
    $str = "<div class='row'>
                <div class='col-lg-12'>
                    <div class='panel panel-default'>
                        <div class='panel-heading'>
                            Danh sách nhân viên
                        </div>
                        <!-- /.panel-heading -->
                        <div class='panel-body'>
                            <div class='table-responsive'>
                                <table class='table table-striped table-bordered table-hover' id='dataTables-example'>
                                    <thead>
                                        <tr>
                                            <th>Họ tên</th>
                                            <th>Email</th>
                                           
                                            <th>Địa chỉ</th>
                                            <th>Điện thoại</th>
                                            <th>Giới tính</th>
                                            <th>Ngày sinh</th>
                                            <th>Số CMND</th>
                                            <th>Phân quyền</th> 
                                            <th></th>
                                            <th></th>   
                                        </tr>
                                    </thead>
                                    <tbody>";
    $result = mysqli_query(getConnect(), "Select * from users");
    while ($row = mysqli_fetch_array($result)) {
        if ($row['gender'] == 1) {
            $sex = "Nam";
        } else if ($row['gender'] == 0) {
            $sex = "Nữ";
        }
        if ($row['role'] == 1) {
            $pq = "Admin";
        } else if ($row['role'] == 2) {
            $pq = "Nhân viên";
        }
        $sinhNhat = $row['birthday'];
        list($year, $month, $day) = split('[/.-]', $sinhNhat);
        $showBirth = $day."-".$month."-".$year;
        $str = $str . "<tr class='odd gradeX'>
                         <td>${row['name']}</td>
                         <td>${row['email']}</td>
                         
                         <td>${row['address']}</td>
                         <td>${row['phone']}</td>    
                         <td class='center'>$sex</td>
                         <td class='center'>$showBirth</td>
                         <td>${row['id_number']}</td>
                         <td>$pq</td>
                         <td align ='center'><button class='btn  btn-primary' data-toggle='modal' data-target='#myModal' value='edit'>Chi tiết</button></td>
                         <td align ='center'><a class='btn  btn-primary' value='edit' href='script/userScript.php?type=reset&email={$row['email']}'>Reset</a></td>
                      </tr>";
    }
    $str = $str . "   </tbody>
                  </table>
                </div> ";
    echo $str;
}

function update() { //update csdl, lấy dữ liệu trong các thẻ input để update vào csdl    
    $email = $_POST['email'];
    $name = $_POST['name'];
    $add = $_POST['add'];
    $gender = $_POST['gender'];
    $insertSex = "";
    if ($gender === 'Nam') {
        $insertSex = "1";
    } else {
        $insertSex = "0";
    }
    $birth = $_POST['birth'];
    list($day, $month, $year) = split('[/.-]', $birth);
    $insertBirth = $year."-".$month."-".$day;
    //$pass = $_POST['pass'];
    $idnum = $_POST['idnum'];
    $role = $_POST['role'];
    $insertRole = "";
    if ($role === 'Admin') {
        $insertRole = "1";
    } else {
        $insertRole = "2";
    }
    $phone = $_POST['phone'];
    mysqli_query(getConnect(), "UPDATE users SET phone='$phone', name='$name' ,id_number='$idnum', email='$email' , address='$add' , gender=$insertSex , birthday='$insertBirth', role=$insertRole WHERE email='$email'");
    header("location: ../users.php?status=updateSuccess");
    //header("Location: ../users.php");
}

function createNewUser() {
    $email = $_POST['email'];
    $name = $_POST['name'];
    $add = $_POST['add'];
    $gender = $_POST['gender'];
    $insertSex = "";
    if ($gender === 'Nam') {
        $insertSex = "1";
    } else {
        $insertSex = "0";
    }
    $birth = $_POST['birth'];
    list($day, $month, $year) = split('[/.-]', $birth);
    $insertBirth = $year."-".$month."-".$day;
    $pass = md5($_POST['pass']);
    $idnum = $_POST['idnum'];
    $role = $_POST['role'];
    $insertRole = "";
    if ($role === 'Admin') {
        $insertRole = "1";
    } else {
        $insertRole = "2";
    }
    $phone = $_POST['phone'];
    $query = "INSERT INTO `quanlylinhkien`.`users` (`id`, `email`, `password`, `name`, `address`, `phone`, `gender`, `birthday`, `id_number`, `role`) VALUES (NULL, '$email', '$pass', '$name', '$add', '$phone', b'$insertSex', '$insertBirth', '$idnum', '$insertRole');";
    mysqli_query(getConnect(), $query);

    header("Location: ../users.php");
}

if (isset($_POST['option'])) {   //click nút Cập nhật thì chạy hàm update
    if ($_POST['option'] == 'Thêm mới') {
        $pass = $_POST['pass'];
        $rePass = $_POST['rePass'];
        if (strcmp($pass, $rePass) === 0) {
            createNewUser();
            header("location: ../users.php?status=createSucces");
        } else {
            header("location: ../users.php?status=createFail");
        }
    } else if ($_POST['option'] == 'Cập nhật') {
        update();
    }
}
?>
